package net.wanglu.www.zzz.rule.sva;

import java.util.HashMap;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.nsubj._NSubjTuple;
import edu.stanford.nlp.trees.TreeGraphNode;
import edu.stanford.nlp.util.StringUtils;

public class EndWithIcsAsSubj extends _SvaRule {

	public static final Pattern EndWithIcs = Pattern.compile("\\bethics\\b|\\belectronics\\b|\\batomics\\b|\\bcivics\\b|\\bgymnastics\\b|\\bphysics\\b|\\bmathematics\\b|\\bmechanics\\b|\\boptics\\b|\\bacoustics\\b|\\bpolitics\\b|\\bstatistics\\b|\\beconomics\\b|\\blinguistics\\b|\\bathletics\\b|\\btactics\\b", Pattern.CASE_INSENSITIVE);
	public final int EndWithIcsSFlag =1;
	public final int EndWithIcsSPFlag =2;
	
	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( super.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		_NSubjTuple nst = (_NSubjTuple) objects[1];
		if ( !"NP".equals(nst.getSubjectAncestor().value())) return Rule.Inapplicable;
		return Rule.Applicable;
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		_NSubjTuple nst =  (_NSubjTuple) objects[1];
		
		if ( this.applicable(preprocessData, nst) == Rule.Inapplicable) return Rule.Inapplicable;
		TreeGraphNode subj = (TreeGraphNode) nst.getSubj();
		TreeGraphNode verb = (TreeGraphNode) nst.getVerb();
		TreeGraphNode cop = (TreeGraphNode) nst.getCop();
		TreeGraphNode closestAux = (TreeGraphNode) nst.getAux();
		
		TreeGraphNode pred = verb;
		if ( cop != null) pred = cop;
		if ( closestAux != null) pred = closestAux;
		String predWord = pred.label().word();
		String predTag = pred.label().tag();

		if ( EndWithIcs.matcher(subj.label().word()).find()) {
			if ( StringUtils.isCapitalized(subj.label().word())) {
				if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.setFlag(this.EndWithIcsSFlag);
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					egg.whereIsWrong = nst;
					return egg;
				}
			} else {
				if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.setFlag(this.EndWithIcsSPFlag);
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					egg.whereIsWrong = nst;
					return egg;
				}							
			}	
		
		}
		return Rule.Inapplicable;
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("以ics结尾表示学科的名词做主语，通常表示单数意义，谓语动词用单数形式。这类名词有:physics，mathematics，mechanics，optics，acoustics，politics，statistics，economics，linguistics，athletics，tactics。");
		sb.append("<br>");
		sb.append("Politics is now taught in all schools.");
		sb.append("<br>");
		sb.append("Economics is a science of the way in which industry and trade produce and use wealth.");
		sb.append("<br>");
		sb.append("注意：当这些动词表示有关方面的活动、情况、见解、原理等意思时，谓语动词需用复数形式。");
		sb.append("<br>");
		sb.append("What are his politics?");
		sb.append("<br>");
		sb.append("The economics of national growth are of the greatest importance to all modern governments.");
		sb.append("<br>");
		sb.append("有少数这类名词指实践活动，或作学科以外的其他解释时，谓语动词用单复数均可。");
		sb.append("<br>");
		sb.append("Politics sometimes has/have an unfortunate influence on character.");
		sb.append("<br>");
		sb.append("My mathematics is/are rather shaky.");
		sb.append("<br>");
		sb.append("Mathematics is/are well taught at that school.");
		sb.append("</p>");
		return sb.toString();
	}
}
